#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        long long sum = 0;
        for (auto e : nums)
        {
            sum += e;
        }
        long long  left = 0, right = 0, target = sum - x, len = -1;
        if (target < 0)
        {
            return -1;
        }
        long long sum1 = 0;
        while (right < nums.size())
        {
            sum1 += nums[right];
            while (sum1 > target)
            {
                sum1 -= nums[left++];
            }
            if (sum1 == target)
                len = max(len, right - left + 1);
            right++;
        }
        return len == -1 ? -1 : nums.size() - len;
    }
};